package com.ruoyi.web.controller.api.shop;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.system.domain.bo.PStoreProductRelationBo;
import com.ruoyi.system.domain.vo.PStoreProductRelationVo;
import com.ruoyi.system.service.IPStoreProductRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * 商品点赞和收藏Controller
 *
 * @author ruoyi
 * @date 2022-08-01
 */
@Api(tags = {"前端-商品点赞和收藏管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/relation")
public class PStoreProductRelationApiController extends BaseController {

    private final IPStoreProductRelationService iPStoreProductRelationService;

    @ApiOperation("前端-查询商品点赞和收藏列表")
    @GetMapping("/list")
    public TableDataInfo<PStoreProductRelationVo> list(PStoreProductRelationBo bo, PageQuery pageQuery) {
        return iPStoreProductRelationService.queryPageLeftList(bo, pageQuery);
    }

    @ApiOperation("前端-获取商品点赞和收藏详细信息")
    @GetMapping("/info/{id}")
    public R<PStoreProductRelationVo> getInfo(@ApiParam("主键")
                                              @NotNull(message = "主键不能为空")
                                              @PathVariable("id") Long id) {
        return R.ok(iPStoreProductRelationService.queryById(id));
    }

    @ApiOperation("前端-新增商品点赞和收藏")
    @RepeatSubmit()
    @PostMapping("/add-relation")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PStoreProductRelationBo bo) {
        return toAjax(iPStoreProductRelationService.insertByBo(bo) ? 1 : 0);
    }

    @ApiOperation("前端-判断是否收藏")
    @GetMapping("/get-flag/{id}")
    public R<Boolean> getFlag(@ApiParam("主键")
                              @NotNull(message = "商品id不能为空")
                              @PathVariable("id") Long id) {
        return R.ok(iPStoreProductRelationService.queryFlagById(id));
    }

    @ApiOperation("前端-取消商品点赞和收藏")
    @GetMapping("/cancle/{id}")
    public R<Void> cancle(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable Long id) {
        iPStoreProductRelationService.cancleFlag(id);
        return R.ok();
    }

    @ApiOperation("前端-判断秒杀商品是否收藏")
    @GetMapping("/get-seckill-flag/{id}")
    public R<Boolean> getSeckillFlag(@ApiParam("主键")
                              @NotNull(message = "商品id不能为空")
                              @PathVariable("id") Long id) {
        return R.ok(iPStoreProductRelationService.querySeckillFlagById(id));
    }

    @ApiOperation("前端-取消秒杀商品点赞和收藏")
    @GetMapping("/cancle-seckill/{id}")
    public R<Void> cancleSeckill(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable Long id) {
        iPStoreProductRelationService.cancleSeckillFlag(id);
        return R.ok();
    }
}
